package cn.iocoder.yudao.module.handguard.controller.admin.journalism;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.journalism.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.journalism.JournalismDO;
import cn.iocoder.yudao.module.handguard.convert.journalism.JournalismConvert;
import cn.iocoder.yudao.module.handguard.service.journalism.JournalismService;

@Tag(name = "管理后台 - 新闻管理")
@RestController
@RequestMapping("/hutai/journalism")
@Validated
public class JournalismController {

    @Resource
    private JournalismService journalismService;

    @PostMapping("/create")
    @Operation(summary = "创建新闻管理")
    @PreAuthorize("@ss.hasPermission('hutai:journalism:create')")
    public CommonResult<Long> createJournalism(@Valid @RequestBody JournalismCreateReqVO createReqVO) {
        return success(journalismService.createJournalism(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新新闻管理")
    @PreAuthorize("@ss.hasPermission('hutai:journalism:update')")
    public CommonResult<Boolean> updateJournalism(@Valid @RequestBody JournalismUpdateReqVO updateReqVO) {
        journalismService.updateJournalism(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除新闻管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:journalism:delete')")
    public CommonResult<Boolean> deleteJournalism(@RequestParam("id") Long id) {
        journalismService.deleteJournalism(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得新闻管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:journalism:query')")
    public CommonResult<JournalismRespVO> getJournalism(@RequestParam("id") Long id) {
        JournalismDO journalism = journalismService.getJournalism(id);
        return success(JournalismConvert.INSTANCE.convert(journalism));
    }

    @GetMapping("/list")
    @Operation(summary = "获得新闻管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:journalism:query')")
    public CommonResult<List<JournalismRespVO>> getJournalismList(@RequestParam("ids") Collection<Long> ids) {
        List<JournalismDO> list = journalismService.getJournalismList(ids);
        return success(JournalismConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得新闻管理分页")
    @PreAuthorize("@ss.hasPermission('hutai:journalism:query')")
    public CommonResult<PageResult<JournalismRespVO>> getJournalismPage(@Valid JournalismPageReqVO pageVO) {
        PageResult<JournalismDO> pageResult = journalismService.getJournalismPage(pageVO);
        return success(JournalismConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出新闻管理 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:journalism:export')")
    @OperateLog(type = EXPORT)
    public void exportJournalismExcel(@Valid JournalismExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<JournalismDO> list = journalismService.getJournalismList(exportReqVO);
        // 导出 Excel
        List<JournalismExcelVO> datas = JournalismConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "新闻管理.xls", "数据", JournalismExcelVO.class, datas);
    }

}
